package edu.geo.containers.geom.impl;

import java.io.Serializable;

import com.vividsolutions.jts.geom.Coordinate;

import edu.geo.containers.geom.Feature;

/**
 * 
 * @author alejandro.sanchez
 *
 */
public class LineSegment extends com.vividsolutions.jts.geom.LineString implements Feature, Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7351912036454198151L;
	
	public static final int DEFAULT_LINE_SEGMENT_ID = -1;
	
	public static final String GEOJSON_TEMPLATE = "{\"type\": \"LineString\", \"coordinates\": [[{0},{1}],"
			+ "[{2},{3}]]}";
	
	int id = DEFAULT_LINE_SEGMENT_ID;
	String strId;
	
	Point beginNode;
	
	Point endNode;
	
	/**
	 * Puntos contenidos en el dominio geométrico.
	 */		
	protected LineSegment(Point beginNode, Point endNode,  com.vividsolutions.jts.geom.GeometryFactory factory) {
		super(factory.getCoordinateSequenceFactory()
				.create(new Coordinate[]{new Coordinate(beginNode.getX(), beginNode.getY()),
						new Coordinate(endNode.getX(), endNode.getY())}), factory);
		this.beginNode = beginNode;
		this.endNode = endNode;
	}

	protected LineSegment(Coordinate[] coordinates,  com.vividsolutions.jts.geom.GeometryFactory factory) {
		super(factory.getCoordinateSequenceFactory().create(coordinates), factory);
		this.beginNode = GeometryFactory.createPoint(coordinates[0].x, coordinates[0].y);
		this.endNode = GeometryFactory.createPoint(coordinates[1].x, coordinates[1].y);
	}
		
	protected LineSegment(int id, Point beginNode, Point endNode, com.vividsolutions.jts.geom.GeometryFactory factory) {
		super(factory.getCoordinateSequenceFactory()
				.create(new Coordinate[]{new Coordinate(beginNode.getX(), beginNode.getY()),
						new Coordinate(endNode.getX(), endNode.getY())}), factory);	
		this.id = id;
		this.beginNode = beginNode;
		this.endNode = endNode;
	}
	
	public int getId() {
		return id;
	}

	public Point getBeginNode() {
		return beginNode;
	}

	public Point getEndNode() {
		return endNode;
	}

	public String getAsWKT() {
		return this.toText();
	}

	public String getAsGML() {
		return null;
	}

	public String getAsGeoJson() {
		String json = "";
		if ( beginNode != null && endNode != null )
		{
			//"{\"type\": \"LineString\", \"coordinates\": [[{0},{1}],"+ "[{2},{3}]]}";
			StringBuffer buffer = new StringBuffer();
			buffer.append("{\"type\": \"LineString\", \"coordinates\": [[");
			buffer.append(beginNode.getX());
			buffer.append(",");
			buffer.append(beginNode.getY());
			buffer.append("],[");
			buffer.append(endNode.getX());
			buffer.append(",");
			buffer.append(endNode.getY());
			buffer.append("]]}");
			json = buffer.toString();
		}
		return json;
	}
	
	public void setCoordinates(Point beginNode, Point endNode)
	{
		this.beginNode = beginNode;
		this.endNode = endNode;
		setCoordinates(this.beginNode, this.endNode);
	}

	@Override
	public void setId(int id) {
		this.id =id;		
	}


	@Override
	public boolean equals(Object obj) {
		LineSegment aLineSegment = (LineSegment)obj;
		return this.beginNode.equals(aLineSegment.getBeginNode())
				&& this.endNode.equals(aLineSegment.getEndNode())
                && this.id == aLineSegment.getId() ;		
	}

	@Override
	public String getStrId() {
		return strId;
	}

	@Override
	public String asSpaghettiRepresentation() {
		StringBuffer buffer = new StringBuffer();			
		buffer.append("[");
		buffer.append(getBeginNode().getId());
		buffer.append(", ");
		buffer.append(getEndNode().getId());
		buffer.append("]");
		return buffer.toString();		
	}

	public void setStrId(String strId) {
		this.strId = strId;		
	}	
}
